package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.user.AddrInfo;
import com.huizhonglm.diap.api.model.user.AddrListParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.user.iView.AddrListIView;

import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-01 17:37
 */
public class AddrListPersenter extends BasePresenter {

    private AddrListIView iView;

    public AddrListPersenter(AddrListIView iView) {
        this.iView = iView;
    }

    public void loadAddrList(){
        AddrListParams params = new AddrListParams();
        params.userId = UserUtil.getUserId();
        API.http().post("addrList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<AddrInfo> list = JSON.parseArray(result, AddrInfo.class);
                    if (!EmptyUtil.isEmpty(list)) {
                        iView.onLoadAddrSuccess(list);
                    } else {
                        iView.onLoadAddrEmpty();
                    }
                } else {
                    iView.onLoadAddrEmpty();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadAddrFailure();
            }
        });
    }
}
